﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
namespace FJASPX.Framwork.Helper
{
    /// <summary>
    ///  文件操作
    /// </summary>
    public sealed class FileHelper
    {

        public FileHelper()
        {
        }
        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="fileName">服务器文件名</param>
        /// <param name="saveFilePath">服务器存放文件目录(含自动创建目录)</param>
        public static void FileCreate(string fileName, string saveFilePath)
        {
            string path = string.Empty;
            DirectoryInfo Dir = new DirectoryInfo(saveFilePath);
            //是否存在目录
            if (!Dir.Exists)
                Dir.Create();
            if (fileName != string.Empty)
            {
                path = string.Format("{0}\\{1}", saveFilePath, fileName);
                FileInfo fileInfo = new FileInfo(path);
                //是否存在文件
                if (!fileInfo.Exists)
                {
                    fileInfo.Create();
                }
            }
            else
                throw new Exception("文件名称错误");
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="filePath">服务器路径(含文件)</param>
        public static void FileDelete(string filePath)
        {
            FileInfo fileInfo = new FileInfo(filePath);
            if (fileInfo.Exists)
                fileInfo.Delete();
            else
                throw new Exception("文件不存在！");
        }
        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="OldPath">服务器原始的文件路径(含文件)</param>
        /// <param name="CopyPath">服务器复制的文件路径</param>
        /// <param name="reWrite">是否允许覆盖</param>
        public static void FileCopy(string OldPath, string CopyPath, bool reWrite)
        {
            FileInfo fileInfo = new FileInfo(OldPath);
            if (fileInfo.Exists)
                fileInfo.CopyTo(CopyPath, reWrite);
        }
        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="OldPath">服务器原始文件路径(含文件)</param>
        /// <param name="MovePath">服务器移动文件路径</param>
        public static void FileMove(string OldPath, string MovePath)
        {
            FileInfo fileInfo = new FileInfo(OldPath);
            if (fileInfo.Exists)
                fileInfo.MoveTo(MovePath);
            else
                throw new Exception("指定文件不存在");
        }
        /// <summary>
        /// 重命名文件
        /// </summary>
        /// <param name="Dir">服务器存放文件路径</param>
        /// <param name="OleName">原始文件名</param>
        /// <param name="NewName">新文件名</param>
        public static void FileReName(string Dir, string OleName, string NewName)
        {
            FileInfo fileInfo = new FileInfo(string.Format("{0}//{1}", Dir, OleName));
            if (fileInfo.Exists)
            {
                fileInfo.MoveTo(string.Format("{0}//{1}", Dir, NewName));
                fileInfo.Delete();
            }
            else
                throw new Exception("指定文件不存在");
        }
        /// <summary>
        /// 删除目录下所有文件
        /// </summary>
        /// <param name="Dir"></param>
        public static void FileDelDir(string Dir)
        {
            if (System.IO.File.Exists(Dir))
            {
                DirectoryInfo myDir = new DirectoryInfo(Dir);
                FileInfo[] files = myDir.GetFiles();
                //遍历删除
                foreach (FileInfo fileName in files)
                {
                    fileName.Delete();
                }
            }
            else
                throw new Exception("目录不存在");
        }
        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="Path">服务器路径</param>
        public static void DirCreate(string Path)
        {
            if (!File.Exists(Path))
                System.IO.Directory.CreateDirectory(Path);
        }
        /// <summary>
        /// 上传文件到服务器
        /// </summary>
        /// <param name="fld">上传控件fileupload</param>
        /// <param name="path">服务器路径(包含文件名称)</param>
        public static void UploadFiletoServer(System.Web.UI.WebControls.FileUpload fld, string path)
        {   //上传
            string returnName = System.IO.Path.GetExtension(fld.FileName);
            string fileName = path + @"/" + returnName;
            fld.SaveAs(fileName);
            //GUID命名
            FileReName(fileName, returnName, Guid.NewGuid().ToString());
            FileDelete(fileName);
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="FilePath">服务器文件路径(包含文件名称)</param>
        public static void DownLoadFiletoServer(string FilePath)
        {
            FileInfo fileInfo = new FileInfo(FilePath);
            if (fileInfo.Exists)
            {
                System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream; charset=gb2312";
                System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpContext.Current.Server.UrlPathEncode(fileInfo.Name));
                System.Web.HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                System.Web.HttpContext.Current.Response.Filter.Close();
                System.Web.HttpContext.Current.Response.WriteFile(fileInfo.FullName);
                System.Web.HttpContext.Current.Response.End();
            }
            else
                throw new Exception("文件不存在");
        }
    }
}
